package com.whmskj.xjlhsc.config;


import cn.dev33.satoken.fun.strategy.SaCorsHandleFunction;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpLogic;
import com.whmskj.xjlhsc.config.properties.FileProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @BelongsProject: XJLHSC
 * @BelongsPackage: com.whmskj.xjlhsc.config
 * @Author: lk
 * @CreateTime: 2025-06-13
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
public class ResourceConfig implements WebMvcConfigurer {

    @Autowired
    private FileProperties fileProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/profile/**").addResourceLocations("file:" + fileProperties.getUploadFolder(),"file:upload/");
    }

    @Bean
    public SaCorsHandleFunction corsHandle() {
        return (req, res, sto) -> {
            res.setHeader("Access-Control-Allow-Origin", "*").setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,PUT").setHeader("Access-Control-Max-Age", "3600").setHeader("Access-Control-Allow-Headers", "*");
            SaRouter.match(SaHttpMethod.OPTIONS).back();
        };
    }

    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForSimple();
    }
}
